﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:url value="/request/info" var="requestInfoUrl" />
<c:url value="/request/delete" var="requestDeleteUrl" />

<script type="text/javascript">
	function requestInfo(requestId) {

		var json = {
			requestId : requestId
		};
		$.ajax({
			url : "${requestInfoUrl}",
			data : JSON.stringify(json),
			type : "POST",
			beforeSend : function(xhr) {
				xhr.setRequestHeader("Accept", "application/json");
				xhr.setRequestHeader("Content-Type", "application/json");
			},
			success : function(obj) {
				$('#result').html("");
				//var obj = JSON.parse(response);
				$('#result').html(
						"request | Автомобиль: " + obj.requestBrand + " "
								+ obj.color + "</br>State Number | Гос.номер: "
								+ obj.stateNumber);
			},
			error : function() {
				alert('failure');
			}
		});
	}

	function requestDelete(requestId) {

		var json = {
			id : requestId
		};
		$.ajax({
			url : "${requestDeleteUrl}/" + requestId + "/ajax",
			type : "POST",
			beforeSend : function(xhr) {
				xhr.setRequestHeader("Accept", "application/json");
				xhr.setRequestHeader("Content-Type", "application/json");
			},
			success : function(obj) {
				if (obj) {
					var tr = $("#c" + requestId);
					tr.css("background-color", "#FF3700");

					tr.fadeOut(400, function() {
						tr.remove();
					});

					$('#result').html(
							"Deleted request: " + obj.requestBrand + "  "
									+ obj.color + " " + obj.stateNumber);
				} else {
					$('#result').html("Error");
				}

			},
			error : function() {
				alert('failure');
			}
		});
	}
</script>

<h1>Список заявок:</h1>

<a href="<c:url value="/request/new" />">Добавить в список</a>

<br />
<br />
<table id="requestsTable" border="1" cellpadding="10" cellspacing="0" 
	class="table table-striped table-bordered">
	<tr>
		<th>ID</th>
		<th>Клиент</th>
		<th>Обслуживающий менеджер</th>
		<th>Статус</th>
		<th>Автомобиль</th>
		<th>Дата взятия</th>
		<th>Дата возврата</th>
		<th>Стоимость аренды</th>
		<th>Стоимость ремонта</th>
		<th>Сообщение для клиента</th>
		<th>Автомобиль повреждён</th>
		<th>Ремонт оплачен</th>
		<th>Аренда оплачена</th>
		<th></th>
	</tr>

	<c:forEach items="${requests}" var="request">
		<c:url value="/request/${request.requestId}" var="viewrequestUrl" />

		<tr id="c${request.requestId}">
			<td>${request.requestId}</td>
			<td>${request.client}</td>
			<td>${request.manager}</td>
			<td>${request.status}</td>
			<td>${request.rentedCar}</td>
			<td>${request.dateOfDelivery}</td>
			<td>${request.dateOfReturn}</td>
			<td>${request.priceOfRent}</td>
			<td>${request.priceOfRepair}</td>
			<td><a href="${viewrequestUrl}">${request.messageForClient}</a></td>
                        <td>${request.damaged}</td>
			<td>${request.paidRepaired}</td>
			<td>${request.paidRented}</td>


			<td><a
				href="<c:url value="/request/edit/${request.requestId}" />">
					Edit </a> &nbsp; <a
				href="<c:url value="/request/delete/${request.requestId}" />">
					Delete </a></td>
		</tr>
	</c:forEach>
</table>

<div id="result"></div>

